function eid(sId) {
	return document.getElementById(sId);
}

var agt = navigator.userAgent.toLowerCase();
var is_ie = (agt.indexOf("msie") != -1);
var localAccess = (document.location.href.substring(0,4).toLowerCase() == 'file');

function getOffsetLeft(el) {
	var ret = 0;
	do {
		ret += el.offsetLeft;
	} while (el = el.offsetParent);
	return ret;
}

function getOffsetTop(el) {
	var ret = 0;
	do {
		ret += el.offsetTop;
	} while (el = el.offsetParent);
	return ret;
}

function scanLeft(txt, pos, delims, inclusive) {
	while (pos > 0) {
		var c = txt.charAt(pos);
		if (delims.indexOf(c) >= 0)
			return (inclusive) ? pos : pos + 1;
		pos--;
	}
	return 0;
}

function scanRight(txt, pos, delims, inclusive) {
	var len = txt.length;
	while (pos < len) {
		var c = txt.charAt(pos);
		if (delims.indexOf(c) >= 0)
			return (inclusive) ? pos : pos - 1;
		pos++;
	}
	return len - 1;
}

String.prototype.stripHTML = function() {
	var s = this;
	s = s.replace(/<br>/g,'\n');
	s = s.replace(/\u2009/g,'');
	s = s.replace(/<.*?>/g,'');
	s = s.replace(/&lt;/g,'<');
	s = s.replace(/&gt;/g,'>');
	s = s.replace(/&amp;/gi,'&');
	return s;
}

function findLineMIDAS(oNode, iLine, _i) {
	if (oNode && oNode.nodeType == 1) {
		_i = _i || [0];	
		if (oNode.tagName == 'BR') {			
			++_i[0];
			if (_i[0] == iLine) {
				return oNode;
			}
		}
		var oChild;
		if (oChild = oNode.firstChild) {
			while (oChild) {
				var oRet = findLineMIDAS(oChild, iLine, _i);
				if (oRet)
					return oRet;
				oChild = oChild.nextSibling;
			}
		}
	}
	return null;
}

function traverseBackwards(oFrom, oLimit, fCallback) {
	// fCallback = function(oNode, bIsText)
	var oNode = oFrom;
	while (oNode && oNode != oLimit) {
		switch (oNode.nodeType) {
			case 3:
				if (fCallback(oNode, true))
					return;				
				break;
			default:
				break;
		}
		// when there is no previous node, climb to parent
		if (!oNode.previousSibling) {
			oNode = oNode.parentNode;
		}
		// do not climb over the limit
		if (oNode == oLimit)
			break;
		// go to previous node
		if (oNode) {
			oNode = oNode.previousSibling;
		}
		// go towards the leaves of a parent node
		while (oNode && oNode.nodeType == 1 && oNode.lastChild) {
			oNode = oNode.lastChild;
		}
		// if the node is a BR tag, increment the Y counter
		if (oNode && oNode.nodeType == 1)
			if (fCallback(oNode, false))
				return;				
	}
}

function findAnchorNodeMIDAS(oNode, iOffset, bEndNode) {
	if (oNode.nodeType != 3) {
		// locate real node (damn you, caret position)
		oNode = oNode.firstChild;
		if (bEndNode)
			--iOffset;
		while (iOffset-- > 0)
			oNode = oNode.nextSibling;
		while (oNode && oNode.nodeType == 1 && oNode.lastChild)
			oNode = oNode.lastChild;
	}
	return oNode;
}

//--- Selection

function Sel() {}

Sel.setRange = function(input, selectionStart, selectionEnd) {
	if (input.setSelectionRange) {
		input.focus();
		input.setSelectionRange(selectionStart, selectionEnd);
	}
	else if (input.createTextRange) {
		var range = input.createTextRange();
		range.collapse(true);
		range.moveEnd('character', selectionEnd);
		range.moveStart('character', selectionStart);
		range.select();
	}
}

Sel.getRange = function(input) {
	if (input.setSelectionRange) {
		return { 'start': input.selectionStart, 'end': input.selectionEnd };
	}
	else 
	if (document.selection) {
		var oRange = document.selection.createRange();
		var oTextRange = oElement.createTextRange();
		oTextRange.setEndPoint('EndToStart', oRange);
		return { 'start': oTextRange.text.length, 'end': (this.start + oRange.text.length) };
	}		
}

Sel.getText = function(input, sel) {
	sel = sel || Sel.getRange(input);
	return input.value.slice(sel.start, sel.end);
}

Sel.replace = function(input, replaceString) {
	if (input.setSelectionRange) {
		var selectionStart = input.selectionStart;
		var selectionEnd = input.selectionEnd;
		input.value = input.value.substring(0, selectionStart)+ replaceString + input.value.substring(selectionEnd);
	
		if (selectionStart != selectionEnd) { 
			Sel.setRange(input, selectionStart, selectionStart + replaceString.length);
		} else {
			Sel.setRange(input, selectionStart + replaceString.length, selectionStart + replaceString.length);
		}
	} else 
	if (document.selection) {
		var range = document.selection.createRange();
		
		if (range.parentElement() == input) {
			var isCollapsed = range.text == '';
			range.text = replaceString;
		
			if (!isCollapsed) {
				range.moveStart('character', -replaceString.length);
				range.select();
			}
		}
	}
}
